package com.tencent.wxcloudrun.uitls;



import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;

import com.tencentcloudapi.sms.v20190711.*;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;

import java.util.Arrays;
/**
 * @author lichongbing
 * @version 1.0.0
 * @createdate 2023/4/1 12:31
 * @description: TODO
 */
public class SmsUtil {

    /**
     * 替换成自己的SecretId
     */
    private static final String SECRET_ID = "AKIDWGo54lceiXLs87v6B1EMFsDKAj1vuCQN";

    /**
     * 替换成自己的SecretKey
     */
    private static final String SECRET_KEY = "g6SYQ9d18tPdbIgngJSM0OUhgRvCDyYq";
    // 替换成自己的短信SDK AppID
    /**
     * 替换成自己的短信SDK AppID
     */
    private static final String SMS_SDK_APP_ID = "1400487655";

    /**
     * 替换成自己的短信签名ID
     */
    private static final String SMS_SIGN_ID = "873738";

    /**
     * 发送短信验证码
     *
     * @param phoneNumber 手机号
     * @param code        验证码
     * @return 发送结果
     */
    public static boolean sendSms(String phoneNumber, String code) {
        boolean isSuccess = false;
        try {
            Credential cred = new Credential(SECRET_ID, SECRET_KEY);
            SmsClient client = new SmsClient(cred, "");
            SendSmsRequest req = new SendSmsRequest();
            // 指定短信接收号码，格式为+国家码+手机号码，例如+8613711112222
            String[] phoneNumberSet = {"+86" + phoneNumber};
            req.setPhoneNumberSet(phoneNumberSet);
            // 指定模板参数，不需要则设置为空串
            String[] templateParamSet = {code};
            req.setTemplateParamSet(templateParamSet);
            // 短信模板ID即短信签名ID
            req.setTemplateID(SMS_SIGN_ID);
            req.setSmsSdkAppid(SMS_SDK_APP_ID);
            req.setSign("越领科技");
            SendSmsResponse resp = client.SendSms(req);
            isSuccess = Arrays.stream(resp.getSendStatusSet())
                    .allMatch(status -> status.getCode().equals("Ok"));
            System.out.println(resp.toString());
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return isSuccess;
    }
}
